<?php

namespace backend\models\search;

use moonland\phpexcel\Excel;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Code;

/**
 * CodeSearch represents the model behind the search form of `common\models\Code`.
 */
class CodeSearch extends Code
{
    public $export;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'status', 'created_at', 'updated_at', 'export', 'is_receive'], 'integer'],
            [['goods_sn', 'security_sn', 'bar_code', 'identity_code', 'link'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Code::find();

        // add conditions that should always apply here
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => ['defaultOrder' => ['id' => SORT_DESC]]
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'is_receive' => $this->is_receive,
            'status' => $this->status,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'goods_sn', $this->goods_sn])
            ->andFilterWhere(['like', 'security_sn', $this->security_sn])
            ->andFilterWhere(['like', 'bar_code', $this->bar_code])
            ->andFilterWhere(['like', 'identity_code', $this->identity_code])
            ->andFilterWhere(['like', 'link', $this->link]);

        if ($this->export) {
            $exportProvider = new ActiveDataProvider([
                'query' => $query,
                'pagination' => [
                    'pageSize' => 50000
                ]
            ]);
            // 修改状态
            Code::updateAll(['is_receive' => 1], ['in', 'id', $query->select('id')->all()]);
            $exportProvider->prepare();
            for ($i=0; $i < $exportProvider->pagination->pageCount; $i++) {
                if ($i !== 0) {
                    $exportProvider->prepare(true);
                }
                $data = $exportProvider->getModels();
                Excel::export([
                    'models' => $data,
                    'fileName' => date('Ymd'),
                    'columns' => ['goods_sn', 'security_sn', 'bar_code', 'identity_code', 'link', 'price'],
                    'headers' => [
                        'id' => '序号',
                        'goods_sn' => '产品识别码',
                        'security_sn' => '防伪码',
                        'bar_code' => '条形码',
                        'identity_code' => '身份码',
                        'link' => '二维码连接',
                        'price' => '价格'
                    ]
                ]);
            }
        }


        return $dataProvider;
    }
}
